//分类业务数据相关
import { onMounted,ref } from "vue";
import { useRoute, onBeforeRouteUpdate } from "vue-router";
import { getTopCategoryAPI } from "@/apis/category";
export function useCategory() {
    //面包屑内容
    const categoryData = ref({});
    const route = useRoute();

    const getCategory = async (id) => {
        // 如何在setup中获取路由参数 useRoute() -> route 等价于this.$route
        const res = await getTopCategoryAPI(id);
        categoryData.value = res.result;
    };
    onMounted(() => getCategory(route.params.id));
    
    // 监听路由参数变化,进行页面切换
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id);
    });
    return { categoryData };
}